package rgbos.forum.webClient;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.Vector;

import rgbos.forum.domain.Post;
import rgbos.forum.exceptions.UserExeception;
import jaxcent.HtmlElement;
import jaxcent.HtmlPara;
import jaxcent.HtmlTable;
import jaxcent.HtmlTableCell;
import jaxcent.HtmlTableRow;
import jaxcent.Jaxception;

public class ShowPosts extends jaxcent.JaxcentPage {

	HtmlTable table = new HtmlTable(this,"postTable");	
	DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
	HtmlPara para = new HtmlPara(this, "header");
	HtmlPara footpara_ = new HtmlPara(this,"footerPara");
	DateFormat df_ = new SimpleDateFormat("dd/MM/yyyy");
	WebClientHandler handler_;

	public ShowPosts() {
		handler_ = WebClientHandler.getWebClientHandler();;
	}
	
	protected void onUnload() {
		try {
			Map sessionData = getAllSessionData(false);
			handler_.unload(sessionData);			
		} catch (Jaxception e) {
			e.printStackTrace();
		}
	}

	protected void onLoad() {
		try {
			table.setCssClass("postTable");
			Map sessionData = getAllSessionData(false);			
			handler_.DisplayHeader(para, sessionData, this); //must be before display footer!
			handler_.displayFooter(footpara_, sessionData, this);
			final String tPath = getCurrentPath();
			final int tIndex = tPath.indexOf("topicId=");
			if (tIndex != -1) {
				try {
					final int topicId = Integer.valueOf(tPath.substring(tIndex+8));
					Vector<Post> postVec = handler_.getControl().enterTopic(topicId);
					if (postVec == null) {
						showMessageDialog("Topic is not available");
						navigate("/showTopics.html");
					}
					int i;
					HtmlElement delPostLink[] = new HtmlElement[postVec.size()];
					String type = handler_.getClientType(sessionData);
					String[] link = new String[1];
					if(!type.equals("Guest")){
						link[0]="<a href=\"NewMessage.html?topicId=" + topicId + "\">Add Post</a><br><br>";
						try {
							HtmlTableRow addPostRow = table.insertRow(0, link);
							HtmlTableCell addPostCell = new HtmlTableCell(this, addPostRow, 0);
							addPostCell.setColSpan(2);
							addPostCell.setCssClass("addPost");
						} catch (Jaxception e) {
							e.printStackTrace();
						}
					}
					i = addPostsTable(sessionData, topicId, postVec, delPostLink, type);
					if(!type.equals("Guest") && i!=0){
						link[0]="<a href=\"NewMessage.html?topicId=" + topicId + "\">Add Post</a>";
						try {
							HtmlTableRow addPostRow = table.insertRow(i*5+5, link);
							HtmlTableCell addPostCell = new HtmlTableCell(this, addPostRow, 0);
							addPostCell.setColSpan(2);
							addPostCell.setCssClass("addPost");
						} catch (Jaxception e) {
							e.printStackTrace();
						}
					}
				} catch (NumberFormatException e) {
					return;
				}
			}
		} catch (Jaxception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	private int addPostsTable(Map sessionData, final int topicId,
			Vector<Post> postVec, HtmlElement[] delPostLink, String type) {
		int i;
		for (i=0; i < postVec.size(); i++) {
			final Post p = postVec.elementAt(i);
			String[] row=new String[1];
			String[] editRow=new String[2];
			//System.out.println("post VEC size= "+postVec.size());
			String qoute="<a href=\"QuoteMessage.html?topicId=" + topicId +"&pid="+p.getId_()+ "\">Quote</a>";
			String edit="<a href=\"EditMessage.html?topicId=" + topicId +"&pid="+p.getId_()+ "\">Edit</a>";
			String delete="<a id=\"delPost"+p.getId_()+"\" href=\"\" onClick=\"return false;\">Delete</a>";
			try {										
				row[0] = handler_.unescape(p.getTitle_());
				int padding = 0;
				if (i == 0)
					padding = 1;
				HtmlTableRow htmlRow1 = table.insertRow(i*5+padding,row);
				HtmlTableCell htmlCell1 = new HtmlTableCell(this, htmlRow1, 0);
				htmlCell1.setCssClass("postHeader");
				htmlCell1.setColSpan(2);
				
				String[] row2 = new String[2];
				row2[0]="<font size=\"2\">Posted by</font><br>"+p.getAuthorName()+"<br>"+df_.format(p.getCreationDate_());				
				row2[1]=handler_.unescape(p.getBody_()).replaceAll("\n", "<BR>");
				HtmlTableRow htmlRow2 = table.insertRow(i*5+1+padding,row2);
				HtmlTableCell htmlCellLeft = new HtmlTableCell(this, htmlRow2, 0);
				HtmlTableCell htmlCellRight = new HtmlTableCell(this, htmlRow2, 1);
				htmlCellLeft.setCssClass("postLeft");					
				htmlCellRight.setCssClass("postRight");
				
				if(type.equals("Guest")){																
					editRow[0] = "&nbsp;";
					editRow[1] = "&nbsp;";
					
					addEditRows(editRow,i,padding);
				}else if(type.equals("User")){
					if(p.getAuthorName().equals((String)sessionData.get("uName"))){									
						editRow[0] = "&nbsp;";
						editRow[1] = edit+" | "+delete;
						
						addEditRows(editRow,i,padding);
						
						delPostLink[i] = addDelLink(topicId, i, p);
						
					}else{
						editRow[0] = "&nbsp;";
						editRow[1] = qoute;
						addEditRows(editRow,i,padding);
					}
				}else if(type.equals("Moderator") | type.equals("Admin")){
					editRow[0] = "&nbsp;";
					editRow[1] = edit+" | "+delete+" | "+qoute;
					addEditRows(editRow,i,padding);
					
					delPostLink[i] = addDelLink(topicId, i, p);								
				}
			} catch (Jaxception e) {
				e.printStackTrace();
			}
		}
		return i;
	}


	private HtmlElement addDelLink(final int topicId, final int i, final Post p) {
		HtmlElement delPostLink = new HtmlElement(this, "delPost"+p.getId_()) {
			protected void onClick( Map pageData ) {
				try {
					boolean sure = showConfirmDialog("Are you sure you want to delete this post?");
					if (sure) {						
						handler_.getControl().deletePost(p.getId_());
						navigate("/showPosts.html?topicId="+topicId);
					}					
				} catch (Jaxception e) {
					e.printStackTrace();
				}
			}
		};
		return delPostLink;
	}
	
	public void addEditRows(String[] editRow, int i, int padding) throws Jaxception {
		HtmlTableRow htmlEditRow = table.insertRow(i*5+3+padding,editRow);
		HtmlTableCell htmlEditCell1 = new HtmlTableCell(this, htmlEditRow, 0);
		HtmlTableCell htmlEditCell2 = new HtmlTableCell(this, htmlEditRow, 1);
		htmlEditCell1.setCssClass("editLeft");
		htmlEditCell2.setCssClass("editRight");
										
		String[] delimeter = {""};								
		HtmlTableRow htmlDelimeterRow = table.insertRow(i*5+4+padding,delimeter);
		HtmlTableCell htmlDelimeterCell1 = new HtmlTableCell(this, htmlDelimeterRow, 0);								
		htmlDelimeterCell1.setCssClass("delimeter");
	}
}
